package com.example.util;

import com.example.dao.AdminDAO;
import com.example.model.CardInfo;
import com.example.model.Deposit;

import javax.swing.table.AbstractTableModel;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

public class DepositTableModel extends AbstractTableModel {

    AdminDAO  adminDAO = new AdminDAO();
    private final String[] columnNames = {"存款ID", "存款名称", "描述"};
    private List<Deposit> deposits = adminDAO.getAllDeposit();


    public DepositTableModel(List<Deposit> deposits) {
        this.deposits = deposits;
    }

    @Override
    public int getRowCount() {
        return deposits.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Deposit deposit = deposits.get(rowIndex);
        return switch (columnIndex) {
            case 0 -> deposit.getSavingId();
            case 1 -> deposit.getSavingName();
            case 2 -> deposit.getDescription();
            default -> null;
        };
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        // 只有ID列不可编辑
        return columnIndex != 0;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        Deposit deposit = deposits.get(rowIndex);
        switch (columnIndex) {
            case 1 -> deposit.setSavingName((String) aValue);
            case 2 -> deposit.setDescription((String) aValue);
        }
        if(!adminDAO.updateDeposit(deposit)){
           return;
        }else{
            fireTableCellUpdated(rowIndex, columnIndex);
        }
    }

}
